package com.mall.service.authority.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mall.model.domain.UmsMenu;
import com.mall.model.domain.UmsResource;
import com.mall.model.domain.UmsRoleResourceRelation;
import com.mall.service.authority.mapper.UmsResourceMapper;
import com.mall.service.authority.service.UmsResourceService;
import com.mall.service.authority.service.UmsRoleResourceRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class UmsResourceServiceImpl extends ServiceImpl<UmsResourceMapper, UmsResource> implements UmsResourceService {

    @Autowired
    private UmsRoleResourceRelationService umsRoleResourceRelationService;

    @Autowired
    private UmsResourceMapper umsResourceMapper;

    @Override
    public List<UmsResource> listResource(Integer roleId) {
        LambdaQueryWrapper<UmsRoleResourceRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UmsRoleResourceRelation::getRoleId, roleId);
        List<UmsRoleResourceRelation> list = umsRoleResourceRelationService.list(queryWrapper);

        List<Long> ids = list.stream().map(item -> {
            return item.getResourceId();
        }).collect(Collectors.toList());

        LambdaQueryWrapper<UmsResource> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(UmsResource::getId,ids);
        List<UmsResource> resourceList = umsResourceMapper.selectList(wrapper);
        return resourceList;
    }
}
